//
//  LogManager.m
//  usbike
//
//  Created by US bike on 2017/7/22.
//  Copyright © 2017年 darvi. All rights reserved.
//

#import "LogManager.h"
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "TextViewLogger.h"

@interface LogManager()

@property (nonatomic, strong) UIButton *btn;
@property (nonatomic, strong) UIButton *clearBtn;
@property (nonatomic, strong) UITextView *logTextView;
@property (nonatomic, strong) NSMutableAttributedString *attrMutableStr;

@end

@implementation LogManager

+ (instancetype) shareInstance {
    static LogManager *obj;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        obj = [[LogManager alloc] init];
    });
    
    return obj;
}

- (instancetype) init {
    if (!self) {
        return nil;
    }
    
    if (self = [super init]) {
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(printLogAction:) name:TextViewLoggerNotificationName object:nil];
        
        return self;
    }
    
    return nil;
}


- (void) logManagerEnable:(Boolean) enable {
    if (enable) {
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [[UIApplication sharedApplication].keyWindow addSubview:self.btn];
            [[UIApplication sharedApplication].keyWindow addSubview:self.clearBtn];
        });
        
        
    }
}


#pragma mark - 懒加载
- (UIButton *) btn {
    if (!_btn) {
        _btn = [UIButton buttonWithType:UIButtonTypeSystem];
        _btn.frame = CGRectMake([[UIScreen mainScreen] bounds].size.width/2 - 10 - 40, 20, 40, 40);
        [_btn setTitle:@"日志" forState:UIControlStateNormal];
        [_btn setTitleColor:[UIColor purpleColor] forState:UIControlStateNormal];
        _btn.titleLabel.font = [UIFont systemFontOfSize:12.0f];
        [_btn addTarget:self action:@selector(logAction) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _btn;
}

- (UIButton *) clearBtn {
    if (! _clearBtn) {
        _clearBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        _clearBtn.frame = CGRectMake([[UIScreen mainScreen] bounds].size.width/2 + 10, 20, 40, 40);
        [_clearBtn setTitle:@"清空" forState:UIControlStateNormal];
        [_clearBtn setTitleColor:[UIColor purpleColor] forState:UIControlStateNormal];
        _clearBtn.titleLabel.font = [UIFont systemFontOfSize:12.0f];
        [_clearBtn addTarget:self action:@selector(clearAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _clearBtn;
}

- (UITextView *) logTextView {
    
    if (!_logTextView) {
        _logTextView = [[UITextView alloc] initWithFrame:CGRectMake(5, 66 + 5, [[UIScreen mainScreen] bounds].size.width - 10, [[UIScreen mainScreen] bounds].size.height - 66 - 10)];
        _logTextView.backgroundColor = [UIColor colorWithWhite:1.0f alpha:0.9];
        [_logTextView setEditable:false];
    }
    
    return _logTextView;
}

- (NSMutableAttributedString *) attrMutableStr {
    
    if (!_attrMutableStr) {
        _attrMutableStr = [NSMutableAttributedString new];
    }
    
    return _attrMutableStr;
}

#pragma mark - 点击事件
- (void) logAction {
    dispatch_async(dispatch_get_main_queue(), ^{
        if (self.logTextView.superview) {
            [self.logTextView removeFromSuperview];
        } else {
            [[UIApplication sharedApplication].keyWindow addSubview:self.logTextView];
            self.logTextView.attributedText = self.attrMutableStr;
            [self.logTextView scrollRangeToVisible:NSMakeRange(self.logTextView.text.length, 0)];
        }
    });
}

- (void) clearAction {
    self.logTextView.text = @"";
    self.attrMutableStr = [NSMutableAttributedString new];
}


- (void) printLogAction:(NSNotification *) notification {
    
    
    
    dispatch_async(dispatch_get_main_queue(), ^{
        
        NSDictionary *userInfo = notification.userInfo;
        UIColor *logColor = [userInfo valueForKey:@"logColor"];
        NSString *logStr = [userInfo valueForKey:@"logStr"];
        
        NSRange range = NSMakeRange(0, logStr.length);
        NSMutableAttributedString *logAttrStr = [[NSMutableAttributedString alloc] initWithString:logStr];
        [logAttrStr addAttribute:NSForegroundColorAttributeName value:logColor range:range];
        
        [self.attrMutableStr appendAttributedString:[[NSAttributedString alloc] initWithString:@"\n\n"]];
        [self.attrMutableStr appendAttributedString:logAttrStr];
        
        if (self.logTextView.superview) {
            self.logTextView.attributedText = self.attrMutableStr;
            [self.logTextView scrollRangeToVisible:NSMakeRange(self.logTextView.text.length, 0)];
            
        }
        
    });
    
    
}


- (void) dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


@end
